import json
from osgeo import ogr


def CreatePointGeojson(json_path):
    # Create a Point
    point = ogr.Geometry(ogr.wkbPoint)
    point.AddPoint(114.323280703,30.58035027)
    print(point.ExportToWkt())

    point_geojson = point.ExportToJson()
    print(point_geojson)

    f=open(json_path,'w')
    f.write(point_geojson)
    f.close()

def CreateMultiPointGeojson(json_path):
    multipoint = ogr.Geometry(ogr.wkbMultiPoint)

    point1 = ogr.Geometry(ogr.wkbPoint)
    point1.AddPoint(114.328070282936,30.5803502731584)
    multipoint.AddGeometry(point1)

    point2 = ogr.Geometry(ogr.wkbPoint)
    point2.AddPoint(114.328032732009,30.5801355212135)
    multipoint.AddGeometry(point2)

    point3 = ogr.Geometry(ogr.wkbPoint)
    point3.AddPoint(114.32819366455,30.5800985745944)
    multipoint.AddGeometry(point3)


    multipoint_geojson = multipoint.ExportToJson()
    print(multipoint_geojson)

    f=open(json_path,'w')
    f.write(multipoint_geojson)
    f.close()

def CreateLineGeojson(json_path):

    # Create a LineString
    line = ogr.Geometry(ogr.wkbLineString)
    line.AddPoint(114.32291507720947,30.578888565037637)
    line.AddPoint(114.32716369628906,30.573715834861623)
    line.AddPoint(114.334716796875,30.57737372262836)
    line.AddPoint(114.32991027832031,30.585058532281685)
    print(line.ExportToWkt())

    line_geojson = line.ExportToJson()
    print(line_geojson)

    with open(json_path,'w',encoding='GBK') as f:
        f.write(line_geojson)
        f.close()

def CreateMultiLineGeojson(json_path):
    multiline = ogr.Geometry(ogr.wkbMultiLineString)

    line1 = ogr.Geometry(ogr.wkbLineString)
    line1.AddPoint(114.328232556581,30.5808582866184)
    line1.AddPoint(114.328199028968,30.5807659207328)
    line1.AddPoint(114.328195005655,30.580650463252)
    line1.AddPoint(114.328221827745,30.5805684883571)
    line1.AddPoint(114.328278154134,30.58049228628)
    multiline.AddGeometry(line1)

    line2 = ogr.Geometry(ogr.wkbLineString)
    line2.AddPoint(114.327886551618, 30.580810949113)
    line2.AddPoint(114.328429698944, 30.5808836872216)
    multiline.AddGeometry(line2)

    line3 = ogr.Geometry(ogr.wkbLineString)
    line3.AddPoint(114.32824730873108,
                   30.580860595764502)
    line3.AddPoint(114.32817086577415,
                   30.581191957647338)
    multiline.AddGeometry(line3)

    multiline_geojson = multiline.ExportToJson()
    print(multiline_geojson)

    with open(json_path,'w',encoding='GBK') as f:
        f.write(multiline_geojson)
        f.close()

def CreatePolygonGeojson(json_path):
    # Create a Polygon
    # Create ring
    ring = ogr.Geometry(ogr.wkbLinearRing)
    ring.AddPoint(114.32291507720947,30.578888565037637)
    ring.AddPoint(114.32716369628906,30.573715834861623)
    ring.AddPoint(114.334716796875,30.57737372262836)
    ring.AddPoint(114.32991027832031,30.585058532281685)
    ring.AddPoint(114.32291507720947,30.578888565037637)
    # Create polygon
    poly = ogr.Geometry(ogr.wkbPolygon)
    poly.AddGeometry(ring)
    print(poly.ExportToWkt())

    polygon_geojson = poly.ExportToJson()
    print(polygon_geojson)

    f=open(json_path,'w')
    f.write(polygon_geojson)
    f.close()

def CreateMultiPolygonGeojson(json_path):
    multipolygon = ogr.Geometry(ogr.wkbMultiPolygon)

    # Create ring #1
    ring1 = ogr.Geometry(ogr.wkbLinearRing)
    ring1.AddPoint(114.32859063148499,
                   30.580943724984383)
    ring1.AddPoint(114.32971715927124,
                   30.580897542093247)
    ring1.AddPoint(114.32917267084122,
                   30.58141248008457)
    ring1.AddPoint(114.32859063148499,
                   30.580943724984383)

    # Create polygon #1
    poly1 = ogr.Geometry(ogr.wkbPolygon)
    poly1.AddGeometry(ring1)
    multipolygon.AddGeometry(poly1)

    # Create ring #2
    ring2 = ogr.Geometry(ogr.wkbLinearRing)
    ring2.AddPoint(114.32831168174744,
                   30.58134320607455)
    ring2.AddPoint(114.32840019464493,
                   30.581250840650867)
    ring2.AddPoint(114.32893931865692,
                   30.58171035776365)
    ring2.AddPoint(114.32882398366928,
                   30.581802722749583)
    ring2.AddPoint(114.32831168174744,
                   30.58134320607455)

    # Create polygon #2
    poly2 = ogr.Geometry(ogr.wkbPolygon)
    poly2.AddGeometry(ring2)
    multipolygon.AddGeometry(poly2)

    # Create ring #3
    ring3 = ogr.Geometry(ogr.wkbLinearRing)
    ring3.AddPoint(114.32827681303023,
                   30.580521150710283)
    ring3.AddPoint(114.32854771614075,
                   30.580752065842397)
    ring3.AddPoint(114.32842701673508,
                   30.580874450639435)
    ring3.AddPoint(114.328236579895,
                   30.580858286618497)
    ring3.AddPoint(114.32820975780487,
                   30.58079132136017)
    ring3.AddPoint(114.32820171117783,
                   30.580687409660836)
    ring3.AddPoint(114.32822316884995,
                   30.580592734460055)
    ring3.AddPoint(114.32827681303023,
                   30.580521150710283)

    # Create polygon #3
    poly3 = ogr.Geometry(ogr.wkbPolygon)
    poly3.AddGeometry(ring3)
    multipolygon.AddGeometry(poly3)

    polygon_multi_geojson = multipolygon.ExportToJson()
    print(polygon_multi_geojson)
    f=open(json_path,'w')
    f.write(polygon_multi_geojson)
    f.close()


if __name__ == '__main__':
    # createShpfile()
    point_json_path = 'data/point.json'
    CreatePointGeojson(point_json_path)
    multiPoint_json_path = 'data/multiPoint.json'
    CreateMultiPointGeojson(multiPoint_json_path)
    line_json_path = 'data/line.json'
    CreateLineGeojson(line_json_path)
    multiLine_json_path = 'data/multiLine.json'
    CreateMultiLineGeojson(multiLine_json_path)
    polygon_json_path = 'data/polygon.json'
    CreatePolygonGeojson(polygon_json_path)
    polygon_mutli_json_path = 'data/multipolygon.json'
    CreateMultiPolygonGeojson(polygon_mutli_json_path)

    #打开并输出创建好的Geojson
    with open(polygon_json_path, "r") as f:
        row_data = json.load(f)
        print(10*'------------')
        print(row_data)